from django.db import models

from Rbac.choices.rbac import UserInfo_status, UserInfo_type, Permission_Only


class Menu(models.Model):
    """
    菜单
    """
    title = models.CharField(max_length=32, unique=True)
    parent = models.ForeignKey("Menu", null=True, blank=True, on_delete=models.CASCADE)
    orderId = models.IntegerField('排序', default=0)
    image = models.CharField('图标', max_length=32, default='fa fa-dashboard')
    type = models.IntegerField('类型')

    # 定义菜单间的自引用关系
    # 权限url 在 菜单下；菜单可以有父级菜单；还要支持用户创建菜单，因此需要定义parent字段（parent_id）
    # blank=True 意味着在后台管理中填写可以为空，根菜单没有父级菜单

    # 循环查找父菜单返回字符串
    def __str__(self):
        # 显示层级菜单
        title_list = [self.title]
        p = self.parent
        while p:
            title_list.insert(0, p.title)
            p = p.parent
        return '-'.join(title_list)
    class Meta:
        managed=False


class Permission(models.Model):
    """
    权限
    """
    name = models.CharField('权限名', max_length=32, unique=True)
    url = models.CharField('权限url', max_length=128, unique=True)
    isOnly = models.IntegerField('菜单下唯一', default=0)
    menu = models.ForeignKey("Menu", null=True, blank=True, on_delete=models.CASCADE)
    image = models.CharField('图标', max_length=32, default='fa fa-circle-o')

    def __str__(self):
        # 显示带菜单前缀的权限
        return '{menu}---{permission}'.format(menu=self.menu, permission=self.name)

    class Meta:
        managed=False


class Role(models.Model):
    """
    角色：绑定权限
    """
    title = models.CharField('角色名', max_length=32, unique=True)
    permissions = models.ManyToManyField("Permission")

    # 定义角色和权限的多对多关系

    class Meta:
        db_table = "t_role"

    def __str__(self):
        return self.title

    class Meta:
        managed=False


# 用户表
class Customer(models.Model):
    '''
    用户表，
    替换原有的userInfo，不生成数据库表
    '''
    # isadmin_choice = (
    #     (0, "否"),
    #     (1, "是")
    # )
    customerid = models.CharField(db_column='CustomerID', primary_key=True, max_length=20,
                                  verbose_name='用户代码')  # Field name made lowercase.
    customername = models.CharField(db_column='CustomerName', max_length=50,
                                    verbose_name='用户名称')  # Field name made lowercase.
    password = models.CharField(db_column='Password', max_length=100, verbose_name='密码')  # Field name made lowercase.
    isactive = models.SmallIntegerField(db_column='IsActive', verbose_name='是否活跃',
                                        default=0)  # Field name made lowercase.
    maxlogined = models.IntegerField(db_column='MaxLogined', verbose_name='最大登录次数')  # Field name made lowercase.
    createtime = models.CharField(
        db_column='CreateTime', max_length=19, blank=True, null=True, verbose_name='创建时间')  # Field name made lowercase.
    updatetime = models.CharField(
        db_column='UpdateTime', max_length=19, blank=True, null=True, verbose_name='更新时间')  # Field name made lowercase.
    # userinfo表继承其父类，故不用写次关联关系
    roles = models.ManyToManyField(verbose_name='拥有的所有角色', to=Role, blank=True, db_constraint=False)

    class Meta:
        managed = False
        db_table = 't_customer'

    def __str__(self):
        return self.customerid






''' 继承自带的用户表

settings.py:
AUTH_USER_MODEL = 'rbac.User'

from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    """
    用户：划分角色
    """
    username = models.CharField(verbose_name='用户', max_length=32, unique=True)

    roles = models.ManyToManyField(verbose_name='角色', to="Role")
    # 定义用户和角色的多对多关系

    def __str__(self):
        return self.username

'''


# 运行环境表
class Environment(models.Model):
    EnvID = models.CharField(
        db_column="EnvID", max_length=20, primary_key=True)
    EnvName = models.CharField(
        db_column="EnvName", max_length=50)
    IP = models.CharField(
        db_column="IP", max_length=40)
    UpdateTime = models.CharField(
        db_column="UpdateTime", max_length=19)
    Type = models.CharField(
        db_column="Type", max_length=1)
    Remark = models.CharField(
        db_column="Remark", max_length=500)

    class Meta:
        db_table = "t_environment"
